package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class IntegralManagementInputConfirmAction extends TmsAction {
	private Logger logger = Logger
			.getLogger(IntegralManagementInputConfirmAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String actionFord = "goCurrent";
		TransferRgActionForm inForm = (TransferRgActionForm) form;
		TransferRgInputViewHelper view = (TransferRgInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		//delete by huangheli 20151012 start
		view.setRemark(inForm.getRemark());
		view.setAccessCardId(inForm.getAccessCardId());
//		view.setLicensePlate(inForm.getLicensePlate());
//		view.setPrice(inForm.getPrice());
//		view.setTransferPrice(inForm.getTransferPrice());
		//delete by huangheli 20151012 end
		if (inForm.getQuxiao() != null && !"".equals(inForm.getQuxiao())) {
			view.setViewBsFlag("1");
			view.setMemberType(0);
			view.setAccessCardNo("");
			actionFord = "goCurrent";
		}
		if (inForm.getSaveInputInfo() != null&& !"".equals(inForm.getSaveInputInfo())) {
			// 结果页
			actionFord = "goResult";
		}
		if (inForm.getSearchBaseInfo() != null&& !"".equals(inForm.getSearchBaseInfo())) {
			// 当前页
			inForm.setSearchBaseInfo("");
			Member member = new Member();
			Attn attn = new Attn();
			if (inForm.getAccessCardId() == null|| "".equals(inForm.getAccessCardId())) {
				setErrorsToRequest(request, TmsConstants.U0005, null);
				return mapping.getInputForward();
			}
			
			IntegralRegister register = new IntegralRegister();
			register = register.getIntegralRegisterInfoBySerial(inForm.getSerial(),2,0);
			if(register != null)
			{
				setErrorsToRequest(request, TmsConstants.IM017, null);
				return mapping.getInputForward();
			}
			
			member = member.getIntegralManagementInfo(inForm.getAccessCardId());
			attn = attn.getIntegralManagementInfo(inForm.getAccessCardId());
			if (member != null) {
				/*if (member.getUserId().equals("2")|| member.getUserId().equals("3")) {
					setErrorsToRequest(request, TmsConstants.IM009, null);
					return mapping.getInputForward();
				}*/
				if (!"1".equals(member.getUserId())) {
					setErrorsToRequest(request, TmsConstants.IM009, null);
					return mapping.getInputForward();
				}
				view.setMemberId(String.valueOf(member.getMemId()));
				view.setMemberName(member.getRealName());
				view.setLegal(member.getLegal());
				view.setPhone(member.getPhone());
				view.setLegalCardNo(member.getCardNo());
				view.setLegalPhoto(member.getCardPhoto());
				view.setAccessCardNo(member.getAccessCardId());
				view.setMemberType(1);
				view.setViewBsFlag("2");
			}  else if (attn != null) {
				/*if (attn.getUserId().equals("2")|| attn.getUserId().equals("3")) {
					setErrorsToRequest(request, TmsConstants.IM009, null);
					return mapping.getInputForward();
				}*/
				if (!"1".equals(attn.getUserId())) {
					setErrorsToRequest(request, TmsConstants.IM009, null);
					return mapping.getInputForward();
				}
				view.setMemberId(String.valueOf(attn.getAttnId()));
				view.setMemberName(attn.getMemberId());
				view.setLegal(attn.getAttnName());
				view.setPhone(attn.getAttnPhone());
				view.setLegalCardNo(attn.getAttnIdCardNo());
				view.setLegalPhoto(attn.getAttnPhoto());
				view.setAccessCardNo(attn.getAccessCardId());
				view.setMemberType(2);
				view.setViewBsFlag("2");
			} else {
				setErrorsToRequest(request, TmsConstants.IM010, null);
				return mapping.getInputForward();
			}
			actionFord = "goCurrent";
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionFord);
	}

}
